#include "Headers.h"
#include "util.h"
#include "extraResource.h"

using namespace std;

double CFunc(vector<string> seg, extraResource& resource)
{
	double l = 0.0;
	double scpVal = 0.0, ngramProb = 0.0, wikiProb = 0.0;
    double score = 0.0;
    util utils;
    string segStr = utils.vectorToStr(seg, ' ');
    wikiProb = resource.getWikiProb(segStr);
    wikiProb = exp(wikiProb);
    ngramProb = resource.getMsProb(segStr, seg.size());

	if (seg.size() == 1)
	{
		l = 1.0 ;
		scpVal = 2 * log(ngramProb);
        score = l * wikiProb * utils.sigmoid(scpVal);
	}
	else
	{
		l = (seg.size()-1)*1.0 / seg.size();
        // scpval
///*
        scpVal = ngramProb * ngramProb;
        double tempScore = 0.0;
        for (int i = 0; i <= seg.size()-2; i ++)
        {
            string seg1Str = utils.vecToStr(seg, ' ', 0, i+1);
            string seg2Str = utils.vecToStr(seg, ' ', i+1, seg.size());
            double seg1Prob = resource.getMsProb(seg1Str, i+1);
            double seg2Prob = resource.getMsProb(seg2Str, seg.size()-i-1);
            tempScore += seg1Prob * seg2Prob;
        }
        tempScore = tempScore / (seg.size()-1);
        scpVal = log(scpVal / tempScore);
//*/
        score = l * wikiProb * utils.sigmoid(scpVal);
	}
	//cout << l;
//    cout << segStr << "(l|wiki|scp): ";
    //cout << l << "\t" << wikiProb << "\t" << scpVal << endl;
	return score;
}

int main()
{
    util utils;
    cout << "Program starts at " << utils.getTime() << endl;
    string datapath = "..//testTools//";
    extraResource resource(datapath);
    string as = "disc";
    string bs = "anne wilson";
    char * ptest = "pointer test";
    cout << ptest << endl;
//    cout << "Anchor num in wiki: " << resource.getWikiGramNum() << endl;
//    cout << as << "\t" << resource.getMsProb(as, 1) << endl;
//    cout << bs << "\t" << resource.getMsProb(bs, 2) << endl;
    map<string, double> hash = resource.getGramMap(3);
    map<string, double>::iterator iter;
    for(iter = hash.begin(); iter != hash.end(); iter++)
    {
        vector<string> gramVec = utils.strToVector(iter->first);
        double score = CFunc(gramVec, resource);
//        cout << iter->second << "\t#" << iter->first << endl;
//        cout << iter->first << "#\t" << score << endl;
    }

    cout << ptest << endl;
    cout << "Program ends at " << utils.getTime() << endl;
    return 0;
}
